package com.xhwl.centiotdata.suppliersystem.zhongkong;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.ZhongKongConstant;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.zhongkong.PedestrianRecordZhongKongDTO;
import com.xhwl.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 09:44
 */
@Service
public class PedestrianZhongKongHandler {
    private static final Logger log = LoggerFactory.getLogger(PedestrianZhongKongHandler.class);

    @Autowired
    private GeneralZhongKongHandler generalZhongKongHandler;

    public List<PedestrianRecordZhongKongDTO> pullRecord(PedestrianIntegrationConfigDTO configDTO) {
        List<PedestrianRecordZhongKongDTO> recordZhongKongDTOList = new ArrayList<>();
        Integer pageNum = 1;
        List<PedestrianRecordZhongKongDTO> recordList = null;
        do {
            recordList = this.requestRecord(pageNum, configDTO);
            if (CollectionUtils.isEmpty(recordList)) {
                break;
            }
            recordZhongKongDTOList.addAll(recordList);
            ++pageNum;
        } while (!CollectionUtils.isEmpty(recordList) && recordList.size() == 500);
        return recordZhongKongDTOList;
    }

    private List<PedestrianRecordZhongKongDTO> requestRecord(Integer pageNum, PedestrianIntegrationConfigDTO configDTO) {
        String path = new StringBuffer()
                .append(configDTO.getIpAndPort())
                .append(ZhongKongConstant.PEDESTRIAN_RECORD_URL)
                .append("?startDate=")
                .append(getIntervalSeconds(null, -10))
                .append("&endDate=")
                .append(getIntervalSeconds(null, 0))
                .append("&pageNo=")
                .append(pageNum)
                .append("&pageSize=500")
                .append("&access_token=")
                .append(configDTO.getToken()).toString();
        JSONObject resultObject = generalZhongKongHandler.get(path);
        String data = resultObject.getString("data");
        if (StringUtils.isEmpty(data)) {
            log.error("get path:{};data is null!", path);
            return null;
        }
        List<PedestrianRecordZhongKongDTO> recordList = JSONObject.parseArray(data, PedestrianRecordZhongKongDTO.class);
        return recordList;
    }

    public String getIntervalMinutes(Date date, int interval) {
        Calendar calendar = Calendar.getInstance();
        if (date == null) {
            calendar.setTime(new Date());
        } else {
            calendar.setTime(date);
        }
        calendar.set(Calendar.MINUTE, (calendar.get(Calendar.MINUTE)) / 10 * 10);
        calendar.set(Calendar.SECOND, 0);
        calendar.add(Calendar.MINUTE, interval);
        Date resultDate = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(resultDate);
    }

    public String getIntervalSeconds(Date date, int interval) {
        Calendar calendar = Calendar.getInstance();
        if (date == null) {
            calendar.setTime(new Date());
        } else {
            calendar.setTime(date);
        }
        calendar.set(Calendar.SECOND, (calendar.get(Calendar.SECOND)) / 10 * 10);
        calendar.add(Calendar.SECOND, interval);
        Date resultDate = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(resultDate);
    }

}
